import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/utils.dart';
import 'package:smart_shoes/define/define_colors.dart';
import 'package:smart_shoes/module/my/view/my_head_view.dart';
import 'package:smart_shoes/router/app_router.dart';

class MyPage extends StatefulWidget {
  const MyPage({super.key});

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  @override
  Widget build(BuildContext context) {
    List<Map<String, dynamic>> items = [
      {
        'imagepath': 'assets/images/shebei-shouji.png',
        'title': '我的设备',
        'issub': 1,
        "gap": 0,
      },
      {
        'imagepath': 'assets/images/fenxiang.png',
        'title': '我的分享',
        'issub': 1,
        "gap": 0,
      },
      {'title': '', 'imagepath': '', 'issub': 1, "gap": 1},
      {
        'imagepath': 'assets/images/yinsixieyi.png',
        'title': '隐私协议',
        'issub': 1,
        "gap": 0,
      },
      {
        'imagepath': 'assets/images/fankui.png',
        'title': '用户反馈',
        'issub': 1,
        "gap": 0,
      },
      {
        'imagepath': 'assets/images/banben.png',
        'title': '当前版本',
        'issub': 0,
        "gap": 0,
      },
    ];
    return Scaffold(
      body: ListView.builder(
        itemCount: items.length + 1,
        itemBuilder: (context, index) {
          if (index == 0) {
            return Container(
              decoration: const BoxDecoration(
                // 添加线性渐变背景
                gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [
                    CustomColors.startColor, // 渐变起始颜色
                    CustomColors.endColor, // 渐变结束颜色
                  ],
                ),
              ),
              child: Center(
                child: GestureDetector(
                  onTap: _customBottomSheet,
                  child: MyHeadView().contentView(),
                ),
              ),
            );
          } else {
            return ListTile(title: _itemEnter(items[index - 1]));
          }
        },
      ),
    );
  }

  _customBottomSheet() {
    Get.toNamed(AppRoutes.accountSettingPage);
    // showModalBottomSheet(
    // context: context,
    // backgroundColor: Colors.transparent,
    // isScrollControlled: true,
    // ,
  }

  Widget _itemEnter(Map data) {
    String title = data['title'];
    int issub = data['issub'];
    String imagepath = data['imagepath'];
    int gap = data['gap'];
    if (gap == 1) {
      return Container(height: 8, color: CustomColors.backgroundColor);
    } else {
      return GestureDetector(
        onTap: () {
          print(title);
          Get.toNamed(AppRoutes.home);
        },
        child: Container(
          padding: const EdgeInsets.only(left: 16, right: 11),
          // color: Colors.amber,
          height: 52.5,
          child: Row(
            children: [
              Image.asset(imagepath, width: 20, height: 20),
              Expanded(
                child: Row(
                  children: [
                    Text(
                      title,
                      style: const TextStyle(
                        color: CustomColors.textColor,
                        fontSize: 13,
                      ),
                    ),
                    const SizedBox(width: 10),
                    Expanded(
                      child: InkWell(
                        // 移除无意义的 setState 调用，若有状态更新需求再添加
                        onTap: () {
                          // 若后续有状态更新逻辑，可在此添加
                          if (title == "我的设备") {
                            Get.toNamed(AppRoutes.myDevice);
                          } else if (title == "我的分享") {
                            Get.toNamed(AppRoutes.myShare);
                          }
                          // Get.toNamed(AppRoutes.myDevice);
                        },
                        child: Row(
                          children: [
                            // 提取 Container 样式为常量
                            const _RedContainer(),
                          ],
                        ),
                      ),
                    ),
                    if (issub == 1)
                      Image.asset(
                        'assets/images/jiantou-up.png',
                        width: 6.5,
                        height: 12,
                        fit: BoxFit.cover,
                      ),
                  ],
                ),
              ),
              // 提取分割线为常量，方便统一修改
              const Divider(height: 0.5, thickness: 0.5, color: Colors.grey),
            ],
          ),
        ),
      );
    }
  }
}

// 假设 ModifyInfoPage 类存在
class ModifyInfoPage extends StatelessWidget {
  final int type;
  final String content;

  const ModifyInfoPage({super.key, required this.type, required this.content});

  @override
  Widget build(BuildContext context) {
    // 提取标题为常量，方便统一修改
    const appBarTitle = 'Modify Info';
    return Scaffold(
      appBar: AppBar(title: Text(appBarTitle)),
      body: Center(child: Text('Type: $type, Content: $content')),
    );
  }
}

/// 一个红色背景的 Container 组件
class _RedContainer extends StatelessWidget {
  const _RedContainer({super.key});

  @override
  Widget build(BuildContext context) {
    // 移除无效的注释代码，若需要设置颜色可取消注释
    return Container(color: Colors.red);
  }
}
